package cc.shimeng.fucloud.controller;

import cc.shimeng.fucloud.entity.Subject;
import cc.shimeng.fucloud.service.SubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/subjects")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @GetMapping
    public List<Subject> getAll() {
        return subjectService.findAll();
    }

    @GetMapping("/{id}")
    public Subject get(@PathVariable Integer id) {
        return subjectService.findById(id);
    }

    @PostMapping
    public Subject create(@RequestBody Subject subject) {
        return subjectService.save(subject);
    }

    @PutMapping("/{id}")
    public Subject update(@PathVariable Integer id, @RequestBody Subject subject) {
        return subjectService.save(subject);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id) {
        subjectService.deleteById(id);
    }
}